#!/usr/bin/env python
# encoding: utf-8
from feature_set.loan.un.utils.loan_un_order_features import *
from feature_set.loan.un.utils.loan_un_installment_features import *


class LoanUnPrePayoffV3(object):

    @staticmethod
    def get_features(rst_data):
        current_info = rst_data['current_info']
        current_apply_time = current_info['current_apply_time']
        current_acq_channel = current_info['current_acq_channel']
        current_loan_amount = current_info['current_loan_amount']
        current_product_code = current_info['current_product_code']
        register_time = current_info['register_time']

        user_history_order = rst_data['user_history_order']
        tmp_order_df = user_history_order.loc[:, ['app_order_id', 'user_id', 'acq_channel', 'product_code',
                                                  'apply_time', 'status', 'app_type', 'product_type']].copy()
        tmp_order_df.rename(columns={'status': 'order_status'}, inplace=True)

        user_history_installment = rst_data['user_history_installment']
        history_merge_installment = tmp_order_df.merge(user_history_installment,
                                                       on=['app_order_id', 'user_id', 'acq_channel'])
        history_merge_installment = history_merge_installment[
            (history_merge_installment.apply_time != current_apply_time) & (
                    history_merge_installment.create_time < current_apply_time)]
        all_bill_features = []
        # 获取还清订单特征
        for feature_type in ['all', 'self_app', 'other_app', 'self_product', 'other_product']:
            first_payoff_features = get_first_payoff_features(history_merge_installment,
                                                              user_history_order,
                                                              payoff_type='pre_payoff',
                                                              current_apply_time=current_apply_time,
                                                              current_loan_amount=current_loan_amount,
                                                              register_time=register_time,
                                                              feature_type=feature_type)
            all_bill_features.append(first_payoff_features)

            last_payoff_features = get_last_payoff_features(history_merge_installment,
                                                            user_history_order,
                                                            payoff_type='pre_payoff',
                                                            current_apply_time=current_apply_time,
                                                            current_loan_amount=current_loan_amount,
                                                            register_time=register_time,
                                                            feature_type=feature_type)
            all_bill_features.append(last_payoff_features)

            day_payoff_features = get_day_payoff_features(history_merge_installment,
                                                          payoff_type='pre_payoff',
                                                          day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                          records_list=[1, 3, 7, 15, 30],
                                                          current_apply_time=current_apply_time,
                                                          feature_type=feature_type)
            all_bill_features.append(day_payoff_features)

            app_prod_payoff_features = get_app_prod_payoff_features(history_merge_installment,
                                                                    payoff_type='pre_payoff',
                                                                    day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                                    records_list=[1, 3, 7, 15, 30],
                                                                    current_apply_time=current_apply_time,
                                                                    current_acq_channel=current_acq_channel,
                                                                    current_product_code=current_product_code,
                                                                    feature_type=feature_type)
            all_bill_features.append(app_prod_payoff_features)

            agg_day_features = get_agg_day_features(history_merge_installment,
                                                    user_history_order,
                                                    payoff_type='pre_payoff',
                                                    day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                    current_apply_time=current_apply_time,
                                                    feature_type=feature_type)
            all_bill_features.append(agg_day_features)

            continue_acc_features = get_continue_acc_feature(history_merge_installment,
                                                             payoff_type='pre_payoff',
                                                             feature_type=feature_type)
            all_bill_features.append(continue_acc_features)

        merged_dict = {}
        for d in all_bill_features:
            merged_dict.update(d)
        merged_df = pd.DataFrame(merged_dict, index=[0])
        merged_df.fillna(-999, inplace=True)
        return merged_df.to_dict(orient='records')[0]
